
import symbolDao from "../../../dao/symbolDao";
import utilfuncs from "../../../../canvas/common/tools/utilfuncs";
var funcs = utilfuncs.UtilFuncs;

import globalConst from "../../../../canvas/common/globalConst";
var global = globalConst.GlobalConst;

import {KeyGenerator} from "../../../../canvas/common/keygenerator";

import { SvgGParser } from "./svgGEleParser";
import { SvgDefParser } from "./svgDefParser";
import { AbsSvgUnitParser } from "./absunitparser";
import { AttachDefs2GraphSvc } from "./attachdef2graphsvc";

class SVGParser {
    constructor() {
        this.minx = 0;
        this.maxx = 0;
        this.miny = 0;
        this.maxy = 0;

        this.svgDefsParser = null;
        this.svgGParser = null;
    }
    parse(bs64FileData) {
        const dxfAsscodeData = atob(bs64FileData);

        const parser = new DOMParser();
        const xmlDoc = parser.parseFromString(dxfAsscodeData, 'text/xml');
        return this.parseXML(xmlDoc);
    }

    parseXML(xmlDoc) {
        let svgRet = null;
        const svgElement = xmlDoc.getElementsByTagName('svg')[0];

        let svgHeight = 1000;
        let svgHeader = this.parseSvgHeader(svgElement);
        if (svgHeader){
            svgHeight = svgHeader.height;
        }

        let childEles = svgElement.children;
        if (childEles.length > 0) {
            for (let i = 0; i < childEles.length; i++) {
                const childEle = childEles[i];
                let tagName = childEle.tagName;
                if (tagName == 'defs') {
                    if (!this.svgDefsParser) {
                        this.svgDefsParser = new SvgDefParser();
                    }
                    this.svgDefsParser.parse(childEle);
                }
                else if (tagName == 'g') {
                    if (!this.svgGParser) {
                        this.svgGParser = new SvgGParser(svgHeight);
                    }
                    this.svgGParser.parse(childEle);
                }
                else{
                    if (!this.svgGParser) {
                        this.svgGParser = new SvgGParser(svgHeight);
                    }
                    this.svgGParser.parse(childEle);
                }
            }
            svgRet = this.processSvg2LGXData(svgHeader);
        } else {
            console.log('No <SVG> elements found in the XML document.');
        }
        return svgRet;
    }

    parseSvgHeader(svgElement){
        let svgHeader = null;
        let left = 0;
        let top = 0;
        let width = 1000;
        let height = 1000
        const attributes = svgElement.attributes;
        for (let i = 0; i < attributes.length; i++) {
            const attr = attributes[i];
            if (attr.name == 'viewBox') {
                let viewBoxCoordStr = attr.value;
                let svc = new AbsSvgUnitParser();
                viewBoxCoordStr = svc.convert2ValidPathCoordStr(viewBoxCoordStr);
                let coordList = viewBoxCoordStr.split(',');
                if (coordList.length == 4){
                    left = parseFloat(coordList[0]);
                    top = parseFloat(coordList[1]);
                    width = parseFloat(coordList[2]);
                    height = parseFloat(coordList[3]);
                    svgHeader = {left,top,width,height};
                }
                svc = null;
            }
        }
        return svgHeader;
    }

    processSvg2LGXData(svgHeader) {
        let symbolDefs = null;
        if (this.svgDefsParser){
            let symbolList = this.svgDefsParser.symbolList;
            symbolDefs = this.parseSvgSymbol(symbolList);
            this.parseSvgGraphs(this.svgGParser.graphList, symbolDefs.symbols);
        }
        else{
            symbolDefs = { units: [], symbols: [] };
        }

        //图形对象属性在defs预定义的，通过此函数赋值给图形对象
        if (this.svgDefsParser){
            AttachDefs2GraphSvc.refSvgInfo2Defs(this.svgGParser.graphList,this.svgDefsParser.linegradientList);
            AttachDefs2GraphSvc.refSvgInfo2Defs(symbolDefs.units,this.svgDefsParser.linegradientList);
        }

        let ret = {symbolDefs,graphs:this.svgGParser.graphList,header:svgHeader};
        return ret;
    }

    parseSvgGraphs(svgGraphList, symbolList) {
        for (let i = 0; i < svgGraphList.length; i++) {
            let graphInfo = svgGraphList[i];
            if (graphInfo.type == 0x0202) {
                let dstType = this.getRefSymbolTypeID(graphInfo.sSymbolName,symbolList);
                if (dstType) {
                    graphInfo.type = dstType;
                    graphInfo.gid = KeyGenerator.gid(dstType);
                    graphInfo.rid = KeyGenerator.rid();
                    graphInfo.refSymbolUUID = graphInfo.sSymbolName;
                }
                else {
                    graphInfo.type = 0;
                }
            }

        }
        return;
    }

    getRefSymbolTypeID(symName,symbolList){
        let dstType = 0;
        symbolList.forEach(sym => {
            if (sym.name == symName){
                dstType = sym.type;
                return ;
            }
        });
        return dstType;
    }

    parseSvgSymbol(symbolList) {
        let typeID = 0x02020000;
        let tAllSymbolUnitLst = new Array();
        let tAllSymbolDefLst = new Array();
        for (let i = 0; i < symbolList.length; i++) {
            let symbol = symbolList[i];
            let tSymbolDef = new symbolDao.TSymbolDef();
            tSymbolDef.name = symbol.name;
            tSymbolDef.type = typeID++;
            tSymbolDef.uuid = symbol.name;
            tAllSymbolDefLst.push(tSymbolDef);

            var units = symbol["units"];
            units.forEach(unitInfo => {
                unitInfo.exflag = true;
                unitInfo.superID = tSymbolDef.uuid;
                unitInfo.superSymType = tSymbolDef.type;
                tAllSymbolUnitLst.push(unitInfo);
            });
        }
        return { units: tAllSymbolUnitLst, symbols: tAllSymbolDefLst };
    }
}

export { SVGParser }